﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using ARS.Control.Custom.Entity.Fields;
using ARS.Control.Custom.Entity.Forms;
using System.Web;

namespace ARS.Control.Custom.Entity.Common
{
    public class ChoiceDialogOtherTableHelper
    {
        private Interface.IChoiceDialogOtherTableModel _model;
        public ChoiceDialogOtherTableHelper(Interface.IChoiceDialogOtherTableModel config)
        {
            this._model = config;
        }
        private string _keyId;
        public string KeyId
        {
            get
            {
                if (_keyId == null)
                {
                    _keyId = HttpContext.Current.Request.QueryString[this._model.RequestKey()];
                    if (string.IsNullOrEmpty(_keyId))
                    {
                        return null;
                    }
                    if (this._model.IsEncryptKey())
                    {
                        _keyId = ARS.PublicTool.DataHandle.Encrypt.DecryptString(_keyId);
                    }
                }
                return _keyId;
            }
            set
            {
                _keyId = value;
            }
        }

        
        /// <summary>
        /// 从数据库查询相关显示
        /// </summary>
        /// <param name="control"></param>
        /// <returns></returns>
        public bool QueryDataShow(Form control)
        {
            if (control.Field.Db_Field_Name == this._model.Db_Field_Name())
            {

                if (!string.IsNullOrEmpty(this.KeyId))
                {
                    ChoicesDialogForm form = control as ChoicesDialogForm;
                    string sql = string.Format(this._model.QuerySql(), this.KeyId);
                    DataTable dt = ARS.DataBase.Provider.DbFactory.DbProvider.Query(sql).Tables[0];
                    if (dt.Rows.Count > 0)
                    {
                        Func<string, string> getstr = (str) =>
                        {
                            if (string.IsNullOrEmpty(str))
                            {
                                return null;
                            }
                            else
                            {
                                return "|" + str + "|";
                            }
                        };
                        form.ValueList = getstr(string.Join("|", from row in dt.AsEnumerable()
                            select
                                ARS.PublicTool.DataHandle.Encrypt.EncryptString(
                                    row[this._model.DataValueField()].ToString())));
                        form.TextList =
                            getstr(string.Join("|", from row in dt.AsEnumerable() select row[this._model.DataTextField()]));
                    }

                }
                control.Field.Value = null;
            }
            return true;
        }
        /// <summary>
        /// 插入数据
        /// </summary>
        /// <param name="form"></param>
        /// <param name="keyId"></param>
        public void InertData(Fields.Field form, string keyId)
        {
            if (form.Value == null)
            {
                return;
            }
            string value = form.Value.ToString();
            if (value == "")
            {
                return;
            }
            string[] list = value.Split('|');
            foreach (string s in list)
            {
                if (s == "")
                {
                    continue;
                }
                string sql = string.Format(this._model.InsertSql(), s, keyId);
                ARS.DataBase.Provider.DbFactory.DbProvider.ExecuteSql(sql);

            };
            form.Value = null;
        }
        /// <summary>
        /// 编辑处理
        /// </summary>
        /// <param name="form"></param>
        /// <param name="control"></param>
        /// <returns></returns>
        public bool AddOrUpdateHandle(ARS.Control.Custom.Entity.Fields.Field form, Widget.EntityControl control)
        {
            if (form.Db_Field_Name == this._model.Db_Field_Name())
            {
                if (control.IsAdd)
                {
                    control.EntityToDbAfterEvent += new ARS.Control.Custom.Entity.Widget.EntityControl.EntityToDbAfterDelegate(control_EntityToDbAfterEvent); ;
                }
                else
                {
                    //string fid = ARS.PublicTool.DataHandle.Encrypt.DecryptString(HttpContext.Current.Request.QueryString["Id"]);
                    string sql = string.Format(this._model.DeleteSql(), this.KeyId);
                    ARS.DataBase.Provider.DbFactory.DbProvider.ExecuteSql(sql);
                    this.InertData(form, this.KeyId);
                }
                return true;
            }
            return false;
        }

        public bool AddOrUpdateHandle(ARS.Control.Custom.Entity.Fields.Field field,int newId)
        {
            string sql = string.Format(this._model.DeleteSql(), newId);
            ARS.DataBase.Provider.DbFactory.DbProvider.ExecuteSql(sql);
            this.InertData(field, newId.ToString()); 
            return true;
        }

        private void control_EntityToDbAfterEvent(List<Fields.Field> list, int newId)
        {
            AddInsertData(list, newId);
        }
        /// <summary>
        /// 添加回调处理
        /// </summary>
        /// <param name="list"></param>
        /// <param name="newid"></param>
        public void AddInsertData(List<Fields.Field> list, int newid)
        {
            foreach (var form in list)
            {
                if (form.Db_Field_Name == this._model.Db_Field_Name())
                {
                    this.InertData(form, newid.ToString());
                }
            }
        }



    }
}
